<?php /** @noinspection ALL */


namespace app\admin\controller;


use think\facade\Cache;
use think\facade\Db;

class OptionProducts {

	function formNeed() {
		$list1 = db::name('option_type')->field('id,name')->select()->all();//产品类型

		$list2 = db::name('task_type')->field('id,cycle')->select()->all();//任务类型

		return json(['code' => 1, 'data' => ['option_type' => $list1, 'task_type' => $list2]]);
	}

	function edit() {
		$input = input();

		if (empty($input['type_id'])) return json(['code' => 0, 'msg' => '请输入分类']);

		if (empty($input['title'])) return json(['code' => 0, 'msg' => '请输入标题']);

		if (empty($input['income'])) return json(['code' => 0, 'msg' => '请输入收益']);

		if (empty($input['basic'])) return json(['code' => 0, 'msg' => '请输入基本']);

		if (empty($input['minor'])) return json(['code' => 0, 'msg' => '请输入次要']);

		if (empty($input['sill'])) return json(['code' => 0, 'msg' => '请输入最少投资']);

		if (empty($input['spread'])) return json(['code' => 0, 'msg' => '请输入点差']);

		if (empty($input['expire_time'])) return json(['code' => 0, 'msg' => '请输入到期时间']);

		if (empty($input['description'])) return json(['code' => 0, 'msg' => '请输入描述']);

		if (empty($input['more_description'])) return json(['code' => 0, 'msg' => '请输入更多描述地址']);

		if ($input['price'] == '') return json(['code' => 0, 'msg' => '请输入价格']);

		if ($input['is_show'] == '') return json(['code' => 0, 'msg' => '请选择状态']);

		if ($input['sort'] == '') return json(['code' => 0, 'msg' => '请输入排序']);

		if ($input['status'] == '') return json(['code' => 0, 'msg' => '请选择状态']);

		if (empty($input['code'])) return json(['code' => 0, 'msg' => '请输入代码']);

		if (empty($input['dingyue_code'])) return json(['code' => 0, 'msg' => '请输入订阅代码']);

		if (empty($input['task_type_id'])) return json(['code' => 0, 'msg' => '请选择任务类型']);

		$data = [
			'type_id' => $input['type_id'],
			'title' => $input['title'],
			'income' => $input['income'],
			'basic' => $input['basic'],
			'minor' => $input['minor'],
			'sill' => $input['sill'],
			'spread' => $input['spread'],
			'expire_time' => $input['expire_time'],
			'description' => $input['description'],
			'more_description' => $input['more_description'],
			'price' => $input['price'],
			'add_time' => time(),
			'is_show' => $input['is_show'],
			'sort' => $input['sort'],
			'code' => $input['code'],
			'dingyue_code' => $input['dingyue_code'],
			'task_type_id' => join(',', $input['task_type_id']),
			'status' => $input['status'],
		];

		if (empty($input['id'])) {

			$r = db::name('option_products')->insert($data);

			if (!$r) {
				return json(['code' => 0, 'msg' => '编辑失败']);
			}

		} else {

			$r = db::name('option_products')->where('id', $input['id'])->update($data);

			if (!$r) {
				return json(['code' => 0, 'msg' => '编辑失败']);
			}
		}
		return json(['code' => 1, 'msg' => '编辑成功']);
	}

	function show($page = 1, $pageSize = 10) {

		$input = input();

		$condition = ' and 1=1';

		if (!empty($input['title'])) {
			$input['title'] = trim($input['title']);
			$condition .= " and option_products.title like '%{$input['title']}%' ";
		}

		if (!empty($input['type_id'])) {
			$condition .= " and option_products.type_id = '{$input['type_id']}' ";
		}

		if (!empty($input['task_type_id'])) {
			$condition .= " and option_products.task_type_id = '{$input['task_type_id']}' ";
		}

		$list = Db::query("select option_products.*,option_type.name as type_name,FROM_UNIXTIME(option_products.add_time,'%Y-%m-%d %H:%i:%s')  as add_time,task_type.cycle from option_products join option_type on option_products.type_id = option_type.id join task_type on task_type.id=option_products.task_type_id where 1" . $condition . " order by option_products.id desc limit " . (($page - 1) * $pageSize) . ",$pageSize");

		$total_num = Db::query("select count(option_products.id) as sum from option_products join option_type on option_products.type_id = option_type.id join task_type on task_type.id=option_products.task_type_id where 1" . $condition);

		$arr=[];
		$cycle1=[];
		for ($i = 0; $i < count($list); $i++) {
			$list[$i]['task_type_id'] = explode(',',$list[$i]['task_type_id']);
			for ($i1 = 0; $i1 < count($list[$i]['task_type_id']); $i1++) {
				$list[$i]['task_type_id'][$i1] = intval($list[$i]['task_type_id'][$i1] );
				$cycle=Db::table('task_type')->where('id', $list[$i]['task_type_id'][$i1])->find();
				array_push($cycle1,$cycle['cycle'].'秒');
			}
			$list[$i]['cycle'] =join(',',$cycle1);
		}
		return json(['code' => 1, 'data' => $list, 'total_num' => $total_num[0]['sum']]);
	}

	function del() {
		$input = input();

		if (!$input['id']) return json(['code' => 0, 'msg' => '数据错误']);

		$user = Db::table('option_products')->where('id', $input['id'])->find();

		if (empty($user)) return json(['code' => 0, 'msg' => '数据不存在']);

		$re = Db::name('option_products')->where('id', $input['id'])->delete();

		if (!$re) {
			return json(['code' => 0, 'msg' => '删除失败']);
		}
		return json(['code' => 1, 'msg' => '删除成功']);
	}


}